$(function () {

    // 加载formSelects
    layui.config({
        base: '/Public/layui-formSelects/dist/'
    }).extend({
        formSelects: 'formSelects-v4'
    });

    layui.use(['layer', 'form', 'formSelects'], function () {
        var layer = layui.layer,
            form = layui.form(),
            formSelects = layui.formSelects;

        formSelects.render('cc');
        formSelects.value('cc', selected_cc);

        // 获取审批规则用户录入数据
        form.on('submit(edit-rule)', function (data) {
            var loading = layer.load(2, {
                shade: [0.5, '#fff'] //0.1透明度的白色背景
            });
            store(data.field).then(function (response) {
               location.href = '/approval/rules';
            });
        });

        $('.add-btn').click(function () {

            // console.log('add btn clicked.');
            var options = $('.layui-form .refrence').eq(0).html(),  // 获取到 options 的 html 内容
                refrence_len = $('.layui-form .refrence').length;   // 获取当前页面有多少级审批
                options = options.replace(/selected|selected=\"\"/g, '');
            var item = [
                '<div class="layui-form-item">',
                    '<label class="layui-form-label">' + (refrence_len + 1) + ' 级审批</label>',
                    '<div class="layui-input-block pull-left" style="width: 500px;">',
                        '<select name="rules[rules][' + (refrence_len + 1) + ']" class="refrence" lay-search>',
                            '<option value="" selected>请选择</option>',
                            options,
                        '</select>',
                    '</div>',
                    '<div class="pull-left">',
                        '<button type="button" class="remove-item layui-btn layui-btn-danger">',
                            '<i class="icon-trash"></i>',
                        '</button>',
                    '</div>',
                '</div>'
            ].join('');
            $(item).insertBefore($('.layui-form .layui-form-item:nth-last-child(4)'));
            form.render('select');
        });

        $('.layui-form').on('click', '.remove-item', function () {
            var item_total = $('.layui-form .refrence').length;
            if (item_total <= 1) {
                layer.msg('至少保留一级审批', {
                    icon: 0,
                    time: 1500
                });
                return;
            }
            $(this).parents('.layui-form-item').remove();
            $('.layui-form .refrence').each(function (index) {
                $(this).attr('name', 'rules[rules][' + (index + 1) + ']');
                $(this).parent().prev().text((index + 1) + ' 级审批');
            });
        });

        function store(data)
        {
            return new Promise(function (resolve, reject) {
                $.post('/approval/ruleStore', data, function (response) {
                    if (response.errno) {
                        layer.closeAll();
                        layer.alert(response.message);
                    } else {
                        resolve(response.data);
                    }
                });
            })
        }

    });

});